package com.reservation.service.impl;

import com.reservation.mapper.AdminHomeMapper;
import com.reservation.service.AdminHomeService;
import com.reservation.vo.admin.home.GetSystemInfo;
import com.reservation.vo.admin.home.RoomStatusCountVO;
import com.reservation.vo.staff.MeetingRoomVO;
import com.reservation.vo.user.home.GetTypeCountVO;
import com.reservation.vo.user.home.GetWeekCountArrVO;
import com.reservation.vo.user.home.GetWeekCountVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j

public class AdminHomeServiceImpl implements AdminHomeService {

    @Autowired
    private AdminHomeMapper adminHomeMapper;


    @Override
    public GetSystemInfo getSystemInfo() {

        GetSystemInfo getSystemInfo = new GetSystemInfo();

        getSystemInfo.setOrderNum(adminHomeMapper.getOrderNum());
        getSystemInfo.setRoomNum(adminHomeMapper.getRoomNumber());
        getSystemInfo.setStaffNum(adminHomeMapper.getStaffNum());
        getSystemInfo.setUserNum(adminHomeMapper.getUserNum());
        return getSystemInfo;
    }

    @Override
    public List<GetTypeCountVO> getTypeCount() {

        List<GetTypeCountVO> list = adminHomeMapper.getTypeCount();
        return list;
    }

    @Override
    public GetWeekCountArrVO getWeekCountArr() {

        List<GetWeekCountVO> list =adminHomeMapper.getWeekCount();

        Map<String, Integer> dateCountMap = list.stream()
                .collect(Collectors.toMap(
                        GetWeekCountVO::getDate,
                        GetWeekCountVO::getCount
                ));

        LocalDate endDate = LocalDate.now().minusDays(1); // 昨天
        LocalDate startDate = endDate.minusDays(6); // 7天前（包含昨天共7天）

        List<String> dateList = new ArrayList<>();
        List<Integer> countList = new ArrayList<>();

        DateTimeFormatter inputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("MM/dd");
        for (LocalDate date = startDate; !date.isAfter(endDate); date = date.plusDays(1)) {
            dateList.add(date.format(outputFormatter));
            countList.add(dateCountMap.getOrDefault(date.format(inputFormatter), 0));
        }
        String[] dateArray = dateList.toArray(new String[0]);
        Integer[] countArray = countList.toArray(new Integer[0]);

        GetWeekCountArrVO arr = new GetWeekCountArrVO(dateArray,countArray);

        return arr;
    }
    
    @Override
    public List<RoomStatusCountVO> getStatusCount() {
        log.info("获取会议室状态统计");
        return adminHomeMapper.getStatusCount();
    }

    @Override
    public List<MeetingRoomVO> getMeetingRoomList() {
        log.info("获取会议室列表");
        List<MeetingRoomVO> meetingRooms = adminHomeMapper.getMeetingRoomList();
        return meetingRooms;
    }

}
